<template>
	<div class="addadress">
		<view class="weui-cells weui-cells_after-title">
			<view class="weui-cell weui-cell_input">
				<view class="weui-cell__hd">
					<view class="weui-label">收货人姓名</view>
				</view>
				<view class="weui-cell__bd">
					<input class="weui-input" placeholder="收货人姓名" v-model="name" />
				</view>
			</view>
			<view class="weui-cell weui-cell_input">
				<view class="weui-cell__hd">
					<view class="weui-label">电话</view>
				</view>
				<view class="weui-cell__bd">
					<input class="weui-input" placeholder="收货人电话" v-model="tel" />
				</view>
			</view>

			<view class="weui-cell weui-cell_input">
				<view class="weui-cell__hd">
					<view class="weui-label">收获地址</view>
				</view>
				<view class="weui-cell__bd">
					<picker mode="region" @change="bindRegionChange" :value="region" :custom-item="customItem">
						<view class="picker">
							<input class="weui-input" :value="region[0]+region[1]+region[2]" />

						</view>
					</picker>
				</view>
			</view>
			<view class="weui-cell weui-cell_input">
				<view class="weui-cell__hd">
					<view class="weui-label">详细地址</view>
				</view>
				<view class="weui-cell__bd">
					<input class="weui-input" placeholder="详细地址" v-model="address" />
				</view>
			</view>
		</view>
		<div>
			<div class="btngroup">
				<button class="weui-btn confim" @click="subaddr()" type="primary">
		确认
		</button>
				<button class="weui-btn cancel" type="default"  @click="cancel()">
		取消
		</button>
			</div>

		</div>
	</div>
</template>

<script>
	import { requests } from "../../../static/js/network"
	export default {
		data() {
			return {
				name: '',
				tel: '',
				customItem: '全部',
				region: ['广东省', '广州市', '海珠区'],
				address: ''
			}
		},
		onLoad() {
			let self=this
			
			if(self.$root.$mp.query.id) {
				requests({
					url: 'https://tp5.blver.cn/api/wechat/getSendAddress',
					method: 'POST',
					header: {
						'content-type': 'application/json',
						'content-type': 'application/x-www-form-urlencoded' // 默认值
					},
					data: {
						id: self.$root.$mp.query.id,

						session3rd: wx.getStorageSync('userinfo').session3rd
					},
					success: function(res) {
						//self.addlist = res.data.msg
						console.log(res.data)
						self.region=[res.data.data.province,res.data.data.city,res.data.data.area];
						self.name=res.data.data.name
						self.tel=res.data.data.tel
						self.address=res.data.data.address
						//self.content=self.classlist[0].goods_content.replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
					}
				})
			}else{
				Object.assign(this.$data, this.$options.data())
			}
		},
		methods: {
			cancel(){
				wx.navigateBack({ //保留当前页面，跳转到应用内的某个页面
								delta: 1
							})
			},
			bindRegionChange: function(e) {
				console.log('picker发送选择改变，携带值为', e.mp.detail.value)
				this.region = e.mp.detail.value

			},
			subaddr() {
				let self = this
				if(this.$root.$mp.query.id) {
					requests({
						url: 'https://tp5.blver.cn/api/wechat/makeSendAddress',
						method: 'POST',
						header: {
							'content-type': 'application/json',
							'content-type': 'application/x-www-form-urlencoded' // 默认值
						},
						data: {
							id: self.$root.$mp.query.id,
							tel: this.tel,
							name: this.name,
							province: this.region[0],
							city: this.region[1],
							area: this.region[2],
							address: this.address,
							session3rd: wx.getStorageSync('userinfo').session3rd
						},
						success: function(res) {
							//self.addlist = res.data.msg
							console.log(res.data.status)
							if(res.data.status=='error'){
								wx.showToast({
						            title: res.data.msg,
						            icon:'none',
						            duration: 2000
						        });
						        return
							}
							
							wx.redirectTo ({ //保留当前页面，跳转到应用内的某个页面
								url: '/pages/goodaddress/main'
							})

							//self.content=self.classlist[0].goods_content.replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
						}
					})
				} else {
					requests({
						url: 'https://tp5.blver.cn/api/wechat/makeSendAddress',
						method: 'POST',
						header: {
							'content-type': 'application/json',
							'content-type': 'application/x-www-form-urlencoded' // 默认值
						},
						data: {
							tel: this.tel,
							name: this.name,
							province: this.region[0],
							city: this.region[1],
							area: this.region[2],
							address: this.address,
							session3rd: wx.getStorageSync('userinfo').session3rd
						},
						success: function(res) {
							console.log(res.data.status)
							if(res.data.status=='error'){
								wx.showToast({
						            title: res.data.msg,
						           icon:'none',
						            duration: 2000
						        });
						        return
							}
							wx.redirectTo ({ //保留当前页面，跳转到应用内的某个页面
								url: '/pages/goodaddress/main'
							})

							//self.content=self.classlist[0].goods_content.replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
						}
					})
				}

			}
		}
	}
</script>

<style>
	.addadress {
		position: relative;
		height: 100vh;
	}
	
	.confim,
	.cancel {
		width: 50%;
		border-radius: 0;
		display: inline-block;
	}
	
	.btngroup {
		position: absolute;
		width: 100%;
		bottom: -6px;
	}
</style>